package com.example.controller;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.example.domain.ExportNianXiuJiaEntity;
import com.example.domain.ExportXsEntity;
import com.example.domain.NianXiuJiaEntity;
import com.example.service.EmailService;
import com.example.service.NianXiuJiaService;
import com.example.utils.config.CommonConfig;
import com.example.utils.poi.ExcelUtil;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@RestController
@RequestMapping("/api")
public class NianXiuJiaController {

	@Autowired
    private NianXiuJiaService nianXiuJiaService;
	@Autowired
    private EmailService emailService;
	/**
	 * 生成年休假表格数据
	 * http://localhost:9000/api/nianxiujia?year=2022&person=某某&deptId=123&date=2021-11-26
	 * @param year
	 * @return
	 */
	@RequestMapping("/nianxiujia")
    public Object queryData(@RequestHeader Map<String, String> headers,String year,String person,String deptId,String date){
	    Map<String, Object> result = new HashMap<>();
//        headers.forEach((key, value) -> {
//            // 日志中输出所有请求头
//            System.out.println(String.format("Header '%s' = %s", key, value));
//        });
	
		if(year == null || "".equals(year)) {
			year = Calendar.getInstance().get(Calendar.YEAR) + "";
		}else {
			year = (Integer.parseInt(year)-1)+""; // 今年提报的是明年的 年休假， 所以年份-1
		}
		 System.out.println("进入年休假接口");
//		log.info("年休假查询参数:年:{},personid:{},deptid:{},date:{}",year,person,deptId,date);
        return nianXiuJiaService.queryQingjiaPerson(year,person,deptId,date);
//		return null;
    }

	/**
	 * 获取泛微系统的部门架构
	 * http://localhost:9000/api/deptTree
	 * @return
	 */
	@RequestMapping("/deptTree")
    public Object deptTree(){
        return emailService.getDeptTreeList();
    }
	
	/**
	 * 导出
	 * http://localhost:9000/api/nianxiujiaexport?year=2022&person=某某&deptId=123&date=2021-11-26
	 * @return
	 */
	@RequestMapping("/nianxiujiaexport")
    public Object nianxiujiaexport(String year,String person,String deptId,String date){
		if(year == null || "".equals(year)) {
			year = Calendar.getInstance().get(Calendar.YEAR) + "";
		}else {
			year = (Integer.parseInt(year)-1)+""; // 今年提报的是明年的 年休假， 所以年份-1
		}
		log.info("导出年休假查询参数:年:{},personid:{},deptid:{},date:{}",year,person,deptId,date);
		List<NianXiuJiaEntity> list = JSONObject.parseArray(nianXiuJiaService.queryQingjiaPerson(year,person,deptId,date), NianXiuJiaEntity.class);
		// 转换成导出对象
		List<ExportNianXiuJiaEntity> retList = list.stream().map(item->{
			ExportNianXiuJiaEntity exportEntity = new ExportNianXiuJiaEntity();
			exportEntity = JSONObject.parseObject(JSONObject.toJSONString(item),ExportNianXiuJiaEntity.class);
			return exportEntity;
			}).collect(Collectors.toList());
        ExcelUtil<ExportNianXiuJiaEntity> util = new ExcelUtil<ExportNianXiuJiaEntity>(ExportNianXiuJiaEntity.class);
        String fileName = util.exportExcel(retList, "人资年休假"+year, CommonConfig.getProfile()+File.separator);
        log.info("excel文件名称:"+CommonConfig.getProfile()+fileName);
		return "/excel/"+fileName;
    }
}

